package com.kgc.crowd.distributioncrowdwebui.controller;

import com.kgc.crow.distributioncrowdcommon.api.MemberOperationRemoteService;
import com.kgc.crow.distributioncrowdcommon.pojo.ResultEntity;
import com.kgc.crow.distributioncrowdcommon.util.CrowdConstant;
import com.kgc.crow.distributioncrowdcommon.vo.MemberSignSuccessVO;
import com.kgc.crow.distributioncrowdcommon.vo.MemberVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class MemberController {

    @Resource
    MemberOperationRemoteService memberOperationRemoteService;

    @RequestMapping("/member/manager/do/login")
    public String logion(MemberVO memberVO, Model model, HttpSession session){
        String loginacct = memberVO.getLoginacct();
        String userpswd = memberVO .getUserpswd();
        //调用远程方法执行登录操作
        ResultEntity<MemberSignSuccessVO> loginResultEntity = memberOperationRemoteService.login(loginacct,userpswd);
        if(loginResultEntity.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,CrowdConstant.MESSAGE_LOGIN_FAILED);
            return "login";
        }
        //如果登录成功，则获取MemberSignSuccessVO对象
        MemberSignSuccessVO memberSignSuccessVO = loginResultEntity.getData();
        //将MemberSignSuccessVO对象存入session域
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER,memberSignSuccessVO);
        return "member";
    }

    @RequestMapping("/iogout")
    public String logout(HttpSession session){
        //获取登录成功的MemberSignSuccessVo
        MemberSignSuccessVO signSuccessVO =(MemberSignSuccessVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        //判断登录状态
        if(signSuccessVO == null){
            return "/index";
        }
        String token = signSuccessVO.getToken();
        ResultEntity<String> logout = memberOperationRemoteService.logout(token);
        if(ResultEntity.FAILED.equals(logout.getResult())){
            throw new RuntimeException(logout.getMessage());
        }
        session.invalidate();
        return "/index";
    }

    @RequestMapping("/do/randomCode")
    @ResponseBody
    public ResultEntity<String> randomCode(String phoneNum){
        ResultEntity<String> stringResultEntity = memberOperationRemoteService.sendCode(phoneNum);
        return stringResultEntity;
    }

    @RequestMapping("/do/register")
    public String register(MemberVO memberVO,Model model){
        ResultEntity<String> register = memberOperationRemoteService.register(memberVO);
        if(register.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,register.getMessage());
            return "reg";
        }else {
            return "login";
        }
    }

    @RequestMapping("/minecrowdfunding")
    public String minecrowdfunding(){
        return "minecrowdfunding";
    }

    @RequestMapping("/member.html")
    public String member(){
        return "member";
    }
}
